package com.gimi.cloud.jimi.manage.consumer.service.sys.impl;

import com.gimi.cloud.jimi.common.enums.ResponseCodeEnum;
import com.gimi.cloud.jimi.common.exception.MsgException;
import com.gimi.cloud.jimi.common.utils.EntityVerification;
import com.gimi.cloud.jimi.common.utils.JsonUtil;
import com.gimi.cloud.jimi.common.utils.reqhead.Response;
import com.gimi.cloud.jimi.common.utils.reqhead.Util;
import com.gimi.cloud.jimi.common.utils.reqhead.Verification;
import com.gimi.cloud.jimi.dto.params.ModifySysRoleParam;
import com.gimi.cloud.jimi.dto.params.RoleListParams;
import com.gimi.cloud.jimi.dto.params.RoleSingleParams;
import com.gimi.cloud.jimi.manage.consumer.feign.SystemFeignService;
import com.gimi.cloud.jimi.manage.consumer.service.sys.RoleService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Created by shazhiwei on 2017/12/15.
 */
@Service
public class RoleServiceImpl implements RoleService{
    private static final Logger logger = LoggerFactory.getLogger(RoleServiceImpl.class);
    private final SystemFeignService systemFeignService;

    public RoleServiceImpl(SystemFeignService systemFeignService){
        this.systemFeignService = systemFeignService;
    }

    @Override
    public Response getRoleList(@RequestBody(required = false) String requestBody, HttpServletRequest request, HttpServletResponse response) {
        return systemFeignService.getRoleList(requestBody);
    }

    @Override
    public Response getRoleSingle(String requestBody, HttpServletRequest request, HttpServletResponse response) throws MsgException {
        logger.info("请求参数===>" + JsonUtil.replaceNow(requestBody));
        RoleSingleParams params = JsonUtil.jsonToBean(requestBody,RoleSingleParams.class);
        Verification verification = EntityVerification.checkEntity(params);
        if(!verification.getCheckResult()){
            Response resp = new Response();
            resp.setResponseCode(ResponseCodeEnum.PARAMETER_ERROR,verification.getMsg());
            return resp;
        }else{
            return systemFeignService.getRoleSingle(params);
        }
    }

    @Override
    public Response getRolePageList(@RequestBody(required = false) String requestBody, HttpServletRequest request, HttpServletResponse response) throws MsgException {
        logger.info("请求参数===>" + JsonUtil.replaceNow(requestBody));
        RoleListParams params = JsonUtil.jsonToBean(requestBody,RoleListParams.class);
        Verification verification = EntityVerification.checkEntity(params);
        if(!verification.getCheckResult()){
            Response resp = new Response();
            resp.setResponseCode(ResponseCodeEnum.PARAMETER_ERROR,verification.getMsg());
            return resp;
        }else{
            return systemFeignService.getRolePageList(params);
        }
    }

    @Override
    public Response addRole(String requestBody, HttpServletRequest request, HttpServletResponse response) throws MsgException {
        logger.info("请求参数===>" + JsonUtil.replaceNow(requestBody));
        ModifySysRoleParam params = JsonUtil.jsonToBean(requestBody,ModifySysRoleParam.class);
        Verification verification = EntityVerification.checkEntity(params);
        if(!verification.getCheckResult()){
            Response resp = new Response();
            resp.setResponseCode(ResponseCodeEnum.PARAMETER_ERROR,verification.getMsg());
            return resp;
        }else{
            return systemFeignService.addRole(params);
        }
    }

    @Override
    public Response editRole(String requestBody, HttpServletRequest request, HttpServletResponse response) throws MsgException {
        logger.info("请求参数===>" + JsonUtil.replaceNow(requestBody));
        ModifySysRoleParam params = JsonUtil.jsonToBean(requestBody,ModifySysRoleParam.class);
        Verification verification = EntityVerification.checkEntity(params);
        if(!verification.getCheckResult()){
            Response resp = new Response();
            resp.setResponseCode(ResponseCodeEnum.PARAMETER_ERROR,verification.getMsg());
            return resp;
        }else{
            return systemFeignService.editRole(params);
        }
    }
}
